package com.budgetme.activities.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.budgetme.R;
import com.budgetme.database.Database;


public class NewBudgetFragment extends Fragment {
	private static final String TAG = NewBudgetFragment.class.getName();
	
	OnAddBudgetSubmitListener mAddSubmitListener;
	
	private EditText mbudgetNameInput;
	private EditText mbudgetBudgetInput;
	private Spinner mbudgetLifecycle;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	Log.d(TAG, "onCreateView");
    	
    	// Inflate the layout for this fragment
    	View fragment = inflater.inflate(R.layout.new_budget_fragment, container, false);
    	
		mbudgetNameInput = (EditText) fragment.findViewById(R.id.add_budget_name);
		mbudgetBudgetInput = (EditText) fragment.findViewById(R.id.add_budget_budget);
		mbudgetLifecycle = (Spinner) fragment.findViewById(R.id.add_budget_lifecycle);
				
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, new String[] {});
		
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		// Apply the adapter to the spinner
		mbudgetLifecycle.setAdapter(adapter);
    	
    	return fragment;
	}
	
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "onAttach");
        
        // Set up any callbacks
        try {
        	mAddSubmitListener = (OnAddBudgetSubmitListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnAddBudgetSubmitListener");
        }
        
    }
    
    @Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    	menu.clear();
	    inflater.inflate(R.menu.new_budget_fragment_menu, menu);
	    super.onCreateOptionsMenu(menu, inflater);
	}
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.budget_list_menu_add:
            	// TODO: Need to send info about the budget
            	mAddSubmitListener.onAddBudgetSubmit();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onDestroy");
		setHasOptionsMenu(true);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
	}
	
	public interface OnAddBudgetSubmitListener {
		public void onAddBudgetSubmit();
	}
	
	

}
